home *** CD-ROM | disk | FTP | other *** search
- /*
- Functions for handling modal dialogs.
-
- 93/03/05 AIH
- - Dialog is set to modal window layer
-
- 92/03/06 AIH
- - Added functions for setting the titles of the ok and cancel buttons
-
- 92/03/02 AIH
- - Changed extra window data into a pointer which is saved in
- the window's refCon field. This simplified a lot of the code.
- Thank goodness for data hiding! It made modifying this
- library a breeze.
-
- 92/02/20 AIH
- - Simplified event handling
- */
-
- #include "DialogLib.h"
- #include "DialogModalLib.h"
- #include "EventLib.h"
- #include "KeyLib.h"
- #include "ResourceLib.h"
- #include "WindowLib.h"
-
- static struct {
- short ok_id; /* 'STR#' resource containing title of ok button */
- short ok_index; /* index to title of ok button */
- short cancel_id; /* 'STR#' resource containing title of cancel button */
- short cancel_index; /* index to title of ok cancel */
- } gModal;
-
- /* initialize the modal dialog */
- void DlgModalInit(DialogPtr dlg)
- {
- CStr255 str;
-
- if (gModal.ok_id) {
- ResStr(gModal.ok_id, gModal.ok_index, str);
- if (DlgItemValid(dlg, ok) && DlgTypeCtl(DlgType(dlg, ok)))
- DlgCtlTitleSet(dlg, ok, str);
- }
- if (gModal.cancel_id) {
- ResStr(gModal.cancel_id, gModal.cancel_index, str);
- if (DlgItemValid(dlg, cancel) && DlgTypeCtl(DlgType(dlg, cancel)))
- DlgCtlTitleSet(dlg, cancel, str);
- }
- }
-
- /* update the modal dialog */
- void DlgModalUpdate(DialogPtr dlg)
- {
- DlgDefaultFrame(dlg, WinHasFocus(dlg) ? black : gray);
- }
-
- /* activate the modal dialog */
- void DlgModalActivate(DialogPtr dlg, Boolean active)
- {
- DlgDefaultFrame(dlg, active ? black : gray);
- }
-
- /* interpret return, enter, command-period, and escape keys */
- void DlgModalKeyDown(DialogPtr dlg, EventRecord *event)
- {
- char key = 0;
- short item = 0;
-
- require(DlgValid(dlg));
-
- /* handle return, enter, and cancel keys */
- key = event->message;
- if (key == returnKey || key == enterKey)
- item = DlgDefault(dlg);
- else if (KeyCancel(event))
- item = cancel;
-
- /* simulate a click in an item (if it's an enabled control) */
- if (item && DlgItemValid(dlg, item)) {
- if (DlgTypeCtl(DlgType(dlg, item)) && DlgCtlEnabled(dlg, item)) {
- DlgFlashButton(dlg, item);
- DlgClick(dlg, item);
- }
- event->what = nullEvent;
- }
- }
-
- /* always return 0 if a filter function is provided for the dialog for
- compatability with the standard ModalDialog */
- TicksType DlgModalAdjustSleep(DialogPtr dlg)
- {
- return(DlgModalFilter(dlg) ? 0 : LONG_MAX);
- }
-
- /* call the dialog's filter function */
- void DlgModalFilterPre(DialogPtr dlg, EventRecord *event)
- {
- WindowPtr window = NULL;
- Boolean stop = false;
- short item = 0;
- short part = 0;
-
- require(DlgValid(dlg));
- if (DlgModalFilter(dlg))
- stop = DlgModalFilter(dlg)(dlg, event, &item, WinExtraPtr(dlg)->filterData);
- if (stop)
- DlgClick(dlg, item);
- else if (event->what == mouseDown) {
- /* beep if clicked outside of permitted locations */
- stop = true;
- switch (FindWindow(event->where, &window)) {
- case inMenuBar:
- stop = false;
- break;
- case inContent:
- case inDrag:
- stop = (window != (WindowPtr) dlg);
- break;
- }
- if (stop) {
- SysBeep(5);
- event->what = nullEvent;
- }
- }
- else if (event->what == keyDown || event->what == autoKey)
- DlgModalKeyDown(dlg, event);
- }
-
- /* return the filter procedure for the dialog */
- DlgModalFilterType DlgModalFilter(DialogPtr dlg)
- {
- return(WinExtraPtr(dlg)->filter);
- }
-
- /* set the filter procedure for the dialog */
- void DlgModalFilterSet(DialogPtr dlg, DlgModalFilterType filter, void *data)
- {
- WinExtraPtr(dlg)->filter = filter;
- WinExtraPtr(dlg)->filterData = data;
- }
-
- /* set the title of the ok button */
- void DlgModalOk(short id, short index)
- {
- gModal.ok_id = id;
- gModal.ok_index = index;
- }
-
- /* set the title of the cancel button */
- void DlgModalCancel(short id, short index)
- {
- gModal.cancel_id = id;
- gModal.cancel_index = index;
- }
-
- /* run a modal dialog */
- short DlgModalRun(DialogPtr dlg)
- {
- require(DlgValid(dlg));
- DlgClick(dlg, 0);
- while (! DlgClicked(dlg))
- EventOne();
- return(DlgClicked(dlg));
- }
-